require "common/Define"
require "framework/BaseController"
require "FairyGUI"
require "MailItem"

VirtualListMain = BaseController:New({mControllerName = "VirtualListMain"});

function VirtualListMain:Main()
	-- body
	VirtualListMain:Awake();
end

function VirtualListMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/VirtualList";
	UIPackage.AddPackage(packageName);
	fgui.register_extension("ui://VirtualList/mailItem", MailItem);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("VirtualList", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	self:OnViewCreated(self.mainComponent);
end

function VirtualListMain:InitView()
	-- body
	self.mainComponent:GetChild("n6").onClick:Add( function () self._list:AddSelection(500, true); end );
	self.mainComponent:GetChild("n7").onClick:Add( function () self._list.scrollPane:ScrollTop(); end );
	self.mainComponent:GetChild("n8").onClick:Add( function () self._list.scrollPane:ScrollBottom(); end );

	self._list = self.mainComponent:GetChild("mailList").asList;
	self._list:SetVirtual();

	self._list.itemRenderer = self.RenderListItem;
	self._list.numItems = 1000;

end

function VirtualListMain.RenderListItem(index, obj)
	-- body
	local item = obj;
	
	item:setFetched(index % 3 == 0);
	item:setRead(index % 2 == 0);
	item:setTime("5 Nov 2015 16:24:33");
	item.title = index .. " Mail title here";
end

function VirtualListMain:OnKeyDown(context)
	if (context.inputEvent.keyCode == KeyCode.Escape) then
		Application.Quit();
	end
end